import scrapy


class QingtingSpider(scrapy.Spider):
    name = 'qingting'
    allowed_domains = ['qingting.fm']
    start_urls = ['https://m.qingting.fm/rank/']

    def parse(self, response):
        # 存储提取到的30个a标签组成的列表对象
        a_list = response.xpath("//div[@class='rank-list']/a")
        # 使用for循环，依次处理每一个a标签对象
        for a_temp in a_list:
            # 排名
            rank_number = a_temp.xpath("./div[@class='badge']/text()")
            # 图片的地址
            img_src = a_temp.xpath("./img/@src")
            # 标题
            title = a_temp.xpath(".//div[@class='title']/text()")
            # 描述
            desc = a_temp.xpath(".//div[@class='desc']/text()")
            # 播放量
            play_number = a_temp.xpath(".//div[@class='info-item'][1]/span/text()")
            # print("---->", rank_number, img_src, title, desc, play_number)
            # print("---->", rank_number.extract(), img_src.extract(), title.extract(), desc.extract(), play_number.extract())
            print("---->", rank_number.extract_first(), img_src.extract_first(), title.extract_first(), desc.extract_first(), play_number.extract_first())

            yield {
                "rank_number": rank_number.extract_first(),
                "img_src": img_src.extract_first(),
                "title": title.extract_first(),
                "desc": desc.extract_first(),
                "play_number": play_number.extract_first()
            }
